<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>6.默认参数的使用技巧</title>
</head>

<body>
  <script>
    //total:总价 year:年数
    /* function avg (total, year) {
      // 老版本写法,year不传值默认 =1；
      //year = year || 1;
      return Math.round(total / year);
    } */
    // 新版本写法，year不传值默认 = 1
    function avg (total, year = 1) {
      return Math.round(total / year);
    }
    console.log(avg(2000, 3));

    // 下下例中当不传递 type 参数时使用默认值 asc ，默认升序排列。
    function sortArray (arr, type = 'asc') {
      return arr.sort((a, b) => type == 'asc' ? a - b : b - a);
    }
    console.log(sortArray([1, 3, 2, 6], 'desc'));

    //默认参数要放在最后面,不然前两个参数是必须传的
    //total:价格,discount:折扣,dis:折后折
      function sum (total, discount = 0, dis = 0) {
        return total * (1 - discount) * (1 - dis);
      }
      console.log(sum(2000, undefined, 0.3));
  </script>
</body>

</html>